import {
  login,
  loginToken,
  logout,
  getInfo,
  getMenuList
} from '@/api/user'
import {
  getToken,
  setToken,
  removeToken
} from '@/utils/auth'
import {
  resetRouter
} from '@/router'
import Layout from '@/layout'
const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    avatar: '',
    accessRoutes: [],
    userInfo: null,
    userName: '',
    nameIsShow: true
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_USERINFO: (state, data) => {
    state.userInfo = data
  },
  SET_NAME: (state, name) => {
    state.name = name
    state.userName = name
  },
  SET_NAMEISSHOW: (state, param) => {
    state.nameIsShow = param
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  }
}

const actions = {
  // user login
  login({
    commit
  }, userInfo) {
    const {
      username,
      password
    } = userInfo
    return new Promise((resolve, reject) => {
      // login({ username: username.trim(), password: password }).then(response => {
      //   const { data } = response
      //   commit('SET_TOKEN', data.token)
      //   setToken(data.token)
      //   resolve()
      // }).catch(error => {
      //   reject(error)
      // })
      login({
        logName: username,
        logPws: password
      }).then(response => {
        // console.log(response)
        commit('SET_TOKEN', response.data.token)
        setToken(response.data.token)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user login
  loginToken({
    commit
  }, userInfo) {
    // const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      loginToken(userInfo).then(response => {
        /* console.log(response)
        console.log(response.data)
        const token = response.data.token
        console.log(token) */
        commit('SET_TOKEN', response.data.token)
        setToken(response.data.token)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({
    commit,
    state
  }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        const {
          data
        } = response
        if (!data) {
          reject('Verification failed, please Login again.')
        }

        commit('SET_NAME', response.data.username)
        commit('SET_AVATAR', response.data.photo)

        commit('SET_USERINFO', response.data)
        resetRouter()
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },
  // get user menu
  getMenuList({
    commit,
    state
  }) {
    return new Promise((resolve, reject) => {
      getMenuList().then(response => {
        // console.log(response)
        // commit('SET_USERINFO', response.data)
        // resetRouter()
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  generateRoutes({
    commit
  }, data) {
    return new Promise(resolve => {
      const accessedRouters = data
      accessedRouters.map(function(item) {
        if (item.component === 'Layout') {
          item.component = Layout
        } else {
          console.log('@/views/' + item.component + '/index.vue')
          // item.component = () => import('@/views/' + item.component + '.vue')
        }
        if (item.redirect === 'noRedirect') {
          item.redirect = null
        }

        // item.children.map(function(child) {
        //   const child_component = child.component
        //   child.component = () => import(`@/views/${child_component}`)
        // })
      })

      accessedRouters.push({
        path: '*',
        redirect: '/404',
        hidden: true
      })
      // commit('SET_ROUTES', accessedRouters)
      resolve(accessedRouters)
    })
  },
  // user logout
  logout({
    commit,
    state
  }) {
    return new Promise((resolve, reject) => {
      // removeToken() // must remove  token  first
      // resetRouter()
      // commit('RESET_STATE')
      // resolve()
      logout(state.token).then(() => {
        removeToken() // must remove  token  first
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  saveToken({
    commit
  }, token) {
    return new Promise((resolve) => {
      commit('SET_TOKEN', token)
      setToken(token)
      resolve()
    })
  },

  // remove token
  resetToken({
    commit
  }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
